const {
  $util,
} = getApp();
Page({
  data: {},
  onLoad(opts) {
    const url = opts.openType === 'books' ? 'bookdetails' : 'getdetails';
    this.setData({
      ...opts,
      url,
    }, () => {
      this.loadDetail();
    });
  },
  loadDetail() {
    const D = this.data;
    $util.request({
      url: `entry/wxapp/edu.databank.${D.url}`,
      data: {
        id: D.id,
      },
      success: (res) => {
        this.setData(res.list);
        res && res.list && res.list.title &&wx.setNavigationBarTitle({
          title: res.list.title,
        });
      },
    });
  },
  opendocument(e) {
    this.setData({
      isdisabled: true,
    });
    wx.showLoading({
      title: '加载中',
    });
    wx.downloadFile({
      url: e.currentTarget.dataset.link,
      success: (res) => {
        const filePath = res.tempFilePath;
        wx.openDocument({
          filePath,
          success: () => {
            this.setData({
              isdisabled: false,
            });
            wx.hideLoading();
            console.log('打开文档成功');
          },
          fail: (error) => {
            console.log(error);
            this.setData({
              isdisabled: false,
            });
            wx.hideLoading();
            wx.showToast({
              title: error,
              icon: 'none',
            });
          },
        });
      },
    });
  },
  onShareAppMessage() {
    let _this = this;
    let path = '/ws_party_const/pages/education/train/network/database/detail?id=' + _this.data.id;
    let res = {
      path,
    };
    return res;
  },
});
